import { dealDataExcept, dealDataRide } from "@/utils/common";
import { goodsCodeRend, goodsNameRend } from "@/utils/tableColumns";
import dayjs from "dayjs";

export function useColumns() {
  // 1备货入库 2直接入库 3备货出库 4直接出库 5配货出库
  const getOptTypeName = optType => {
    let str = "备货入库";
    if (optType == 2) {
      str = "直接入库";
    } else if (optType == 3) {
      str = "备货出库";
    } else if (optType == 4) {
      str = "直接出库";
    } else if (optType == 5) {
      str = "配货出库";
    }
    return str;
  };

  // 库存流水
  const kclsColumns: TableColumnList = [
    // {
    //   type: "expand",
    //   slot: "expand",
    //   fixed: "left",
    //   width: "30px"
    // },
    {
      label: "业务日期",
      prop: "storeCode",
      formatter: ({ gmtCreate }) => dayjs(gmtCreate).format("YYYY-MM-DD"),
      width: 120
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      width: 200,
      align: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "业务类型", // 业务类型
      prop: "optType",
      formatter: ({ optType }) => getOptTypeName(optType),
      width: 100
    },
    {
      label: "业务单据编号",
      prop: "bizCode",
      slot: "bizCode",
      minWidth: 120
    },
    {
      label: "仓库名称",
      prop: "depositoryName",
      minWidth: 120
    },
    {
      label: "发生数量",
      prop: "specNum",
      minWidth: 100,
      formatter: ({ optType, specNum }) =>
        [3, 4, 5].includes(optType) ? "-" + specNum : specNum
    },
    {
      label: "发生成本", // 发生成本: specNum * specPrice 库存成本: leaveNum * specPrice
      prop: "supplierName",
      minWidth: 100,
      formatter: ({ optType, specNum, specPrice }) =>
        [3, 4, 5].includes(optType)
          ? "-" + dealDataExcept(dealDataRide(specNum || 0, specPrice || 0)) ||
            "--"
          : dealDataExcept(dealDataRide(specNum || 0, specPrice || 0)) || "--"
    },
    {
      label: "库存成本",
      prop: "supplierName",
      minWidth: 100,
      formatter: ({ leaveNum, avgBuyPrice }) =>
        dealDataExcept(dealDataRide(leaveNum || 0, avgBuyPrice || 0)) || "--"
    },
    {
      label: "库存余量",
      prop: "leaveNum",
      minWidth: 100
    },
    {
      label: "业务时间",
      prop: "storeCode",
      formatter: ({ gmtCreate }) =>
        dayjs(gmtCreate).format("YYYY-MM-DD HH:mm:ss"),
      width: 100
    }
  ];

  // 锁定库存
  const sdkcColumns: TableColumnList = [
    {
      label: "业务日期",
      prop: "storeCode",
      formatter: ({ gmtCreate }) => dayjs(gmtCreate).format("YYYY-MM-DD"),
      width: 120
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 150,
      align: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "goodsName",
      minWidth: 155,
      align: "left",
      cellRenderer: goodsCodeRend
    },
    {
      label: "业务类型", // 业务类型
      prop: "optType",
      formatter: ({ optType }) => getOptTypeName(optType),
      minWidth: 100
    },
    {
      label: "业务单据编号",
      prop: "bizCode",
      slot: "bizCode",
      minWidth: 170
    },
    {
      label: "仓库名称",
      prop: "depositoryName",
      minWidth: 120
    },
    {
      label: "锁定数量",
      prop: "specNum",
      minWidth: 100
    }
  ];

  // 在途库存
  const ztkcColumns: TableColumnList = [
    {
      label: "业务日期",
      prop: "storeCode",
      formatter: ({ gmtCreate }) => dayjs(gmtCreate).format("YYYY-MM-DD"),
      width: 120
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 150,
      align: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "goodsName",
      minWidth: 150,
      align: "left",
      cellRenderer: goodsCodeRend
    },
    {
      label: "业务类型", // 业务类型
      prop: "optType",
      formatter: ({ optType }) => getOptTypeName(optType),
      minWidth: 100
    },
    {
      label: "业务单据编号",
      prop: "bizCode",
      slot: "bizCode",
      minWidth: 170
    },
    {
      label: "仓库名称",
      prop: "depositoryName",
      minWidth: 120
    },
    {
      label: "在途数量",
      prop: "specNum",
      minWidth: 100
    },
    {
      label: "单价",
      prop: "specPrice",
      minWidth: 100,
      formatter: ({ specPrice }) => dealDataExcept(specPrice) || "--"
    },
    {
      label: "总价",
      prop: "specNum",
      minWidth: 100,
      formatter: ({ specNum, specPrice }) =>
        dealDataExcept(dealDataRide(specNum || 0, specPrice || 0)) || "--"
    }
  ];

  // 各仓库存
  const ggkcColumns: TableColumnList = [
    {
      type: "expand",
      slot: "expand",
      fixed: "left",
      width: "30px"
    },
    {
      label: "商品名称/规格名称",
      prop: "specName",
      slot: "goodsName",
      minWidth: 200,
      align: "left"
    },
    {
      label: "仓库名称",
      prop: "depositoryName",
      slot: "depositoryName",
      minWidth: 150
    },
    {
      label: "实际库存",
      prop: "actualNum",
      minWidth: 120
    },
    {
      label: "可用库存",
      prop: "canUseNum",
      minWidth: 120
    },
    {
      label: "在途库存",
      prop: "sendNum",
      minWidth: 120
    }
  ];

  // 业务单据
  const ywdjColumns: TableColumnList = [
    {
      type: "expand",
      slot: "expand",
      fixed: "left",
      width: "30px"
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      minWidth: 200,
      align: "left",
      cellRenderer: goodsNameRend
    },
    {
      label: "商品编码/规格编码",
      prop: "specName",
      minWidth: 200,
      align: "left",
      cellRenderer: goodsCodeRend
    },
    {
      label: "数量",
      prop: "goodsAmount",
      minWidth: 120
    },
    {
      label: "单价",
      prop: "price",
      minWidth: 120,
      formatter: ({ price }) => dealDataExcept(price) || 0
    },
    {
      label: "总计",
      prop: "totalPrice",
      minWidth: 120,
      formatter: ({ totalPrice }) => dealDataExcept(totalPrice) || 0
    }
  ];

  // 业务单据 - 完成
  const ywdjWcColumns: TableColumnList = [
    {
      type: "expand",
      slot: "expand",
      fixed: "left",
      width: "30px"
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      slot: "goodsName",
      align: "left",
      minWidth: 200
    },
    {
      label: "商品编码/规格编码",
      prop: "specName",
      slot: "specName",
      align: "left",
      minWidth: 200
    },
    {
      label: "数量",
      prop: "goodsAmount",
      minWidth: 120
    },
    {
      label: "单价",
      prop: "price",
      minWidth: 120,
      slot: "price"
    },
    {
      label: "总计",
      prop: "totalPrice",
      minWidth: 120,
      formatter: ({ totalPrice }) => dealDataExcept(totalPrice) || 0
    }
  ];

  // 业务单据 - 锁定
  const ywdjSdColumns: TableColumnList = [
    {
      type: "expand",
      slot: "expand",
      fixed: "left",
      width: "30px"
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      slot: "goodsName",
      align: "left",
      minWidth: 200
    },
    {
      label: "商品编码/规格编码",
      prop: "specName",
      slot: "specName",
      align: "left",
      minWidth: 200
    },
    {
      label: "商品数量",
      prop: "goodsAmount",
      minWidth: 120
    },
    {
      label: "已出库数量",
      prop: "quantityShipped",
      minWidth: 120,
      formatter: ({ quantityShipped }) => quantityShipped || 0
    },
    {
      label: "已入库数量",
      prop: "storeAmount",
      minWidth: 120,
      formatter: ({ storeAmount }) => storeAmount || 0
    }
  ];

  // 业务单据 - 中途
  const ywdjZtColumns: TableColumnList = [
    {
      type: "expand",
      slot: "expand",
      fixed: "left",
      width: "30px"
    },
    {
      label: "商品名称/规格名称",
      prop: "goodsName",
      slot: "goodsName",
      align: "left",
      minWidth: 200
    },
    {
      label: "商品编码/规格编码",
      prop: "specName",
      slot: "specName",
      align: "left",
      minWidth: 200
    },
    {
      label: "商品数量",
      prop: "goodsAmount",
      minWidth: 120
    },
    {
      label: "待入库数量",
      prop: "quantityShipped",
      minWidth: 120,
      formatter: ({ quantityShipped }) => quantityShipped || 0
    },
    {
      label: "已入库数量",
      prop: "storeAmount",
      minWidth: 120,
      formatter: ({ storeAmount }) => storeAmount || 0
    },
    {
      label: "单价",
      prop: "price",
      minWidth: 120,
      slot: "price"
    },
    {
      label: "总计",
      prop: "totalPrice",
      minWidth: 120,
      slot: "totalPrice"
      // formatter: ({ goodsAmount, price }) =>
      //   dealDataExcept(dealDataRide(goodsAmount || 0, price || 0)) || "--"
    }
  ];

  return {
    kclsColumns,
    ggkcColumns,
    ywdjColumns,
    ywdjWcColumns,
    ywdjSdColumns,
    ywdjZtColumns,
    sdkcColumns,
    ztkcColumns
  };
}
